﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace BBS.Data
{
    /// <summary>
    /// 数据库中表的基础操作（增、删、改、查）
    /// </summary>
    class BaseTable
    {
        private string _connectionString = string.Empty;
        private string _tableName = string.Empty;

        public BaseTable(string connstring,string tableName)
        {
            _connectionString = connstring;
            _tableName = tableName;
        }
        /// <summary>
        /// 查询整张表
        /// </summary>
        /// <returns>表内容</returns>
        public DataTable SelectTable()
        {
            return SelectTable(null);
        }
        /// <summary>
        /// 根据条件查询整张表
        /// </summary>
        /// <returns>表内容</returns>
        public DataTable SelectTable(string where)
        {
            string sql;
            if (string.IsNullOrEmpty(where))
            {
                sql = string.Format("select * from {0}", _tableName);
            }
            else
            {
                sql = string.Format("select * from {0} {1}", _tableName, where);
            }
            return Access.ExecuteDataset(_connectionString, CommandType.Text, sql).Tables[0];
        }
        /// <summary>
        /// 执行语句
        /// </summary>
        /// <param name="sql">被执行语句</param>
        /// <returns>受印象行数</returns>
        public int ExecuteNonQuery(string sql)
        {
            return Access.ExecuteNonQuery(_connectionString, CommandType.Text, sql);
        }
        /// <summary>
        /// 执行语句
        /// </summary>
        /// <param name="sql">被执行语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>受印象行数</returns>
        public int ExecuteNonQuery(string sql,params OleDbParameter[] parameters)
        {
            return Access.ExecuteNonQuery(_connectionString, CommandType.Text, sql, parameters);
        }
        /// <summary>
        /// 执行语句
        /// </summary>
        /// <param name="sql">被执行语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>返回datatable</returns>
        public DataTable ExecuteDataTable(string sql, params OleDbParameter[] parameters)
        {
            return Access.ExecuteDataset(_connectionString, CommandType.Text, sql, parameters).Tables[0];
        } 

    }
}
